package net.hhk.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import net.hhk.connect.HTTPConnect;
import net.hhk.data.Constant;
import net.hhk.data.Tweet;
import net.hhk.data.User;

import android.content.Context;
import android.graphics.Color;

public class UserManager {
	private List<User> users;

	private Context context = null;
	private Boolean isFollowing;

	/**
 * 
 */
	public UserManager(final Context context, Boolean follow) {
		this.context = context;
		this.isFollowing = follow;
	}

	/**
	 * Get Tweetter feed
	 * 
	 * @param tokens
	 * @return
	 */
	public List<User> getSocialFeed(String url, int id, int page, int per_page) {
		// Lets ignore userName for sake of this demo

		users = new ArrayList<User>();
		// Lets simulate we are fetching things from server and thats why 3
		// second delay
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		users = loadUser(url, id, page, per_page);

		return users;
	}

	public List<User> loadUser(String url, int id, int page, int per_page) {
		List<Tweet> tweets = new ArrayList<Tweet>();
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("page", Integer
				.toString(page)));
		postParameters.add(new BasicNameValuePair("per_page", Integer
				.toString(per_page)));
		if (id > 0)
			postParameters.add(new BasicNameValuePair("userid", Integer
					.toString(id)));
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(url, postParameters);
		System.out.println(result);
		try {
			JSONObject obj = new JSONObject(result);
			int length = obj.length();
			for (int i = 0; i < length; i++) {
				JSONObject object = obj.getJSONObject(Integer.toString(i));
				int userid = object.getInt("id");
				String urlImagine = object.getString("avatar");
				Boolean status;
				// if (isFollowing)
				// status = true;
				// else
				status = object.getBoolean("status");

				String userName = object.getString("username");
				String fullName = object.getString("screenName");

				User new_user = new User(userid, userName, fullName, urlImagine);
				new_user.setIsfollowed(status);

				users.add(new_user);
			}

			// Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
			// }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}

	public List<User> getUsers() {
		return users;
	}
	public List<User> getUserByTweet(String url, int id, int page, int per_page) {
		// Lets ignore userName for sake of this demo

		users = new ArrayList<User>();
		// Lets simulate we are fetching things from server and thats why 3
		// second delay
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		users = loadUserByTweet(url, id, page, per_page);

		return users;
	}

	public List<User> loadUserByTweet(String url, int id, int page, int per_page) {
		List<Tweet> tweets = new ArrayList<Tweet>();
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("page", Integer
				.toString(page)));
		postParameters.add(new BasicNameValuePair("per_page", Integer
				.toString(per_page)));
		if (id > 0)
			postParameters.add(new BasicNameValuePair("tweetid", Integer
					.toString(id)));
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(url, postParameters);
		System.out.println(result);
		try {
			JSONObject obj = new JSONObject(result);
			int length = obj.length();
			for (int i = 0; i < length; i++) {
				JSONObject object = obj.getJSONObject(Integer.toString(i));
				int userid = object.getInt("id");
				String urlImagine = object.getString("avatar");
				Boolean status;
				// if (isFollowing)
				// status = true;
				// else
				status = object.getBoolean("status");

				String userName = object.getString("username");
				String fullName = object.getString("screenName");

				User new_user = new User(userid, userName, fullName, urlImagine);
				new_user.setIsfollowed(status);

				users.add(new_user);
			}

			// Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
			// }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}

}
